package com.example.exp_3

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MaterialTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    ArtSpaceScreen()
                }
            }
        }
    }
}

@Composable
fun ArtSpaceScreen() {
    var currentArtworkIndex by remember { mutableIntStateOf(0) }

    val artworks = listOf(
        Artwork(
            imageRes = R.drawable.cat01,
            title = "可爱的小猫 01",
            artist = "猫咪摄影师",
            year = "2025"
        ),
        Artwork(
            imageRes = R.drawable.cat02,
            title = "可爱的小猫 02",
            artist = "猫咪摄影师",
            year = "2025"
        ),
        Artwork(
            imageRes = R.drawable.cat03,
            title = "可爱的小猫 03",
            artist = "猫咪摄影师",
            year = "2025"
        ),
        Artwork(
            imageRes = R.drawable.cat04,
            title = "可爱的小猫 04",
            artist = "猫咪摄影师",
            year = "2025"
        ),
        Artwork(
            imageRes = R.drawable.cat05,
            title = "可爱的小猫 05",
            artist = "猫咪摄影师",
            year = "2025"
        )
    )

    val currentArtwork = artworks[currentArtworkIndex]

    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(32.dp),
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center
    ) {
        // 艺术作品展示区域
        Card(
            elevation = CardDefaults.cardElevation(defaultElevation = 8.dp),
            modifier = Modifier.padding(bottom = 32.dp)
        ) {
            Box(
                modifier = Modifier.size(300.dp),
                contentAlignment = Alignment.Center
            ) {
                Image(
                    painter = painterResource(id = currentArtwork.imageRes),
                    contentDescription = currentArtwork.title,
                    modifier = Modifier.size(280.dp),
                    contentScale = ContentScale.Crop
                )
            }
        }

        // 艺术作品信息
        Card(
            modifier = Modifier
                .fillMaxWidth()
                .padding(bottom = 32.dp),
            elevation = CardDefaults.cardElevation(defaultElevation = 4.dp)
        ) {
            Column(
                modifier = Modifier.padding(16.dp),
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Text(
                    text = currentArtwork.title,
                    style = MaterialTheme.typography.headlineSmall,
                    fontWeight = FontWeight.Bold
                )
                Text(
                    text = "${currentArtwork.artist} (${currentArtwork.year})",
                    style = MaterialTheme.typography.bodyMedium,
                    color = MaterialTheme.colorScheme.onSurfaceVariant
                )
            }
        }

        // 导航按钮
        Row(
            modifier = Modifier.fillMaxWidth(),
            horizontalArrangement = Arrangement.SpaceBetween
        ) {
            Button(
                onClick = {
                    if (currentArtworkIndex > 0) {
                        currentArtworkIndex--
                    } else {
                        currentArtworkIndex = artworks.size - 1
                    }
                },
                modifier = Modifier.weight(1f)
            ) {
                Text("Previous")
            }

            Spacer(modifier = Modifier.width(16.dp))

            Button(
                onClick = {
                    if (currentArtworkIndex < artworks.size - 1) {
                        currentArtworkIndex++
                    } else {
                        currentArtworkIndex = 0
                    }
                },
                modifier = Modifier.weight(1f)
            ) {
                Text("Next")
            }
        }
    }
}

data class Artwork(
    val imageRes: Int,
    val title: String,
    val artist: String,
    val year: String
)

@Preview(showBackground = true)
@Composable
fun ArtSpaceScreenPreview() {
    MaterialTheme {
        ArtSpaceScreen()
    }
}